package com.wrb.wechat.service;

import com.wrb.wechat.entity.TextMessage;
import com.wrb.wechat.utils.WechatMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author wrb
 * @date 2019/11/20 17:00
 */
@Slf4j
@Service
public class WechatService {

		public String processRequest(HttpServletRequest request) {
			Map<String, String> map = WechatMessageUtil.xmlToMap(request);
			log.info(String.valueOf(map));
			// 发送方帐号（一个OpenID）
			String fromUserName = map.get("FromUserName");
			// 开发者微信号
			String toUserName = map.get("ToUserName");
			// 消息类型
			String msgType = map.get("MsgType");
			// 默认回复一个"success"
			String responseMessage = "success";
			// 对消息进行处理
			if (WechatMessageUtil.MESSAGE_TEXT.equals(msgType)) {// 文本消息
				TextMessage textMessage = new TextMessage();
				textMessage.setMsgType(WechatMessageUtil.MESSAGE_TEXT);
				textMessage.setToUserName(fromUserName);
				textMessage.setFromUserName(toUserName);
				textMessage.setCreateTime(System.currentTimeMillis());
				textMessage.setContent("我已经受到你发来的消息了");
				responseMessage = WechatMessageUtil.textMessageToXml(textMessage);
			}
			log.info(responseMessage);
			return responseMessage;

		}

}
